# -*- coding: utf-8 -*-

from time import time, strftime

import wx
import wx.lib.ogl as ogl


class RLine(ogl.LineShape):
    
    def __init__(self, canvas, fromShape, toShape, name=None):
        ogl.LineShape.__init__(self)
        #line = ogl.LineShape()
        self.SetCanvas(canvas)
        self.SetPen(wx.BLACK_PEN)
        self.SetBrush(wx.BLACK_BRUSH)
        self.SetSpline(True)
        self.AddArrow(ogl.ARROW_ARROW)
        self._initialised=True
        
        self.fromShape = fromShape
        self.toShape = toShape
        self.name = name
        if fromShape == toShape:
            self.MyMakeLineControlPoints(4)
        else:
            self.MakeLineControlPoints(4)
        
        fromShape.AddLine(self, toShape)
        
        self.canvas = canvas
        
        self.fromShape = fromShape
        self.toShape = toShape
        
        evthandler = LineEvtHandler(canvas.log, canvas.frame)
        evthandler.SetShape(self)
        evthandler.SetPreviousHandler(self.GetEventHandler())
        self.SetEventHandler(evthandler) 
        
    def MyMakeLineControlPoints(self, n):
        # Make a given number of control points (minimum of two).
        self._lineControlPoints = []
        print "make it"
        for _ in range(0, n):
            point = wx.RealPoint(10 * _, 10*_)
            print point
            print _
            self._lineControlPoints.append(point)        
    

class LineEvtHandler(ogl.ShapeEvtHandler):
    def __init__(self, log, frame):
        ogl.ShapeEvtHandler.__init__(self)
        self.log = log
        self.statbarFrame = frame

    def UpdateStatusBar(self, shape):
        x, y = shape.GetX(), shape.GetY()
        width, height = shape.GetBoundingBoxMax()
        self.statbarFrame.SetStatusText("Pos: (%d, %d)  Size: (%d, %d)" %
                                        (x, y, width, height))


    def OnLeftClick(self, x, y, keys=0, attachment=0):
        print "on left click"
        shape = self.GetShape()
        
        if isinstance(shape, RLine):
            #shape.SetPen(wx.RED_PEN)
            #shape.SetBrush(wx.RED_BRUSH)
            pass
        else:
            print type(shape)
        #shape.SetName(time())
        for region in shape.GetRegions():
            if region.GetName() == 'col':
                #pass
                #region.ClearText()
                #print ">>>>>>>>in col"
                #region.SetName("name\nset")
                region.SetText("name\nset")
            """
            for item in region.GetFormattedText():
                print item.GetText()
                print item.SetText(item.GetText() + '_a')
            print region.GetName()
            print region.GetText()
            print "end region for"
            """
        canvas = shape.GetCanvas()
        
        dc = wx.ClientDC(canvas)
        
        canvas.PrepareDC(dc)
        #Redraw(dc)
        
        if shape.Selected():
            #shape.name=time.time()
            shape.Select(False, dc)
            canvas.Redraw(dc)
        else:
            redraw = False
            shapeList = canvas.GetDiagram().GetShapeList()
            toUnselect = []

            for s in shapeList:
                if s.Selected():
                    # If we unselect it now then some of the objects in
                    # shapeList will become invalid (the control points are
                    # shapes too!) and bad things will happen...
                    toUnselect.append(s)

            shape.Select(True, dc)

            if toUnselect:
                for s in toUnselect:
                    s.Select(False, dc)

                canvas.Redraw(dc)
        #ogl.ShapeEvtHandler.OnLeftClick(self, )
        #self.OnSizingEndDragLeft(0, x, y, keys, attachment)
        #canvas.Refresh()
        self.UpdateStatusBar(shape)



    def OnLeftDoubleClick(self, x, y, keys = 0, attachment = 0):
        print "double click"
        shape = self.GetShape()
        fromShape = shape.fromShape
        fromShape.RemoveLine(shape)
        #shape.canvas.MyDeleteShape(shape)
        #shape.Show(False)
        canvas = shape.canvas
        canvas.Refresh()
        shape.Delete()
        
        

    def OnEndDragLeft(self, x, y, keys=0, attachment=0):
        print "on end drag left"
        shape = self.GetShape()
        ogl.ShapeEvtHandler.OnEndDragLeft(self, x, y, keys, attachment)

        if not shape.Selected():
            self.OnLeftClick(x, y, keys, attachment)

        self.UpdateStatusBar(shape)


    def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
        print "On Sizing End Drag Left"
        ogl.ShapeEvtHandler.OnSizingEndDragLeft(self, pt, x, y, keys, attch)
        self.UpdateStatusBar(self.GetShape())


    def OnMovePost(self, dc, x, y, oldX, oldY, display):
        print "on move post"
        ogl.ShapeEvtHandler.OnMovePost(self, dc, x, y, oldX, oldY, display)
        self.UpdateStatusBar(self.GetShape())


    def OnRightClick(self, *dontcare):
        print "on right click"
        #name = "%s" %(time())
        print self.GetShape() #.name = name
        #self.log.WriteText("%s\n" % self.GetShape())
    """
    def  OnDrawControlPoints(self, dc):
        shape = self.GetShape()
        if isinstance(shape, wx.lib.ogl._lines.LineShape):
            
            print "draw is line"
  
    def OnEraseControlPoints(self, dc):        
        shape = self.GetShape()
        if isinstance(shape, wx.lib.ogl._lines.LineShape):
            
            print "erase is line"
    """        
